import {take, call, put, fork, cancel} from 'redux-saga/effects';
import AuthManager from '../services/auth'
import * as Actions from '../actions/index'
import * as Types from '../actions/ActionTypes.js'
import * as api from '../services/student';
import {message} from 'antd';
import callServiceApi from './utils';

// 手机/邮箱 登录
function* watchProfile() {
  while (true) {
    yield take(Types.STUDENT_PROFILE.REQUEST);
    yield fork(studentProfile);
  }
}

function* watchStudentsQuery() {
  while (true) {
    const { payload } = yield take(Types.STUDENTS_QUERY.REQUEST);
    yield fork(studentsQuery, payload);
  }
}
function* watchStudentsDelete() {
  while (true) {
    const { payload } = yield take(Types.STUDENTS_DELETE.REQUEST);
    yield fork(studentsDelete, payload);
    const action = yield take([Types.STUDENTS_DELETE.SUCCESS]);
    if(action.type == Types.STUDENTS_DELETE.SUCCESS) {
      message.info("操作成功！");
      yield put(Actions.studentsDeleteNotify()(payload));
    }
  }
}

function* studentProfile() {
  yield fork(callServiceApi, Actions.studentProfile, api.profile, {});
}

function* studentsQuery(param) {
  yield fork(callServiceApi, Actions.studentsQuery, api.studentsQuery, param);
}

function* studentsDelete(param) {
  yield fork(callServiceApi, Actions.studentsDelete, api.studentsDelete, param);
}

export default function*() {
  yield [
    fork(watchProfile),
    fork(watchStudentsQuery),
    fork(watchStudentsDelete),

  ];
}
